package com.wiscamp.ninechapters.panels.adapter.resources;

import com.wiscamp.ninechapters.panels.application.command.CreateOrganizationCommand;
import com.wiscamp.ninechapters.panels.application.command.UpdateOrganizationCommand;
import com.wiscamp.ninechapters.panels.application.services.OrganizationAppService;
import com.wiscamp.ninechapters.panels.application.vo.OrganizationVO;
import cube.ddd.adapter.BaseController;
import cube.ddd.adapter.RestResponse;
import cube.ddd.application.command.DeleteCommand;
import cube.ddd.application.command.DisableCommand;
import cube.ddd.application.command.EnableCommand;
import cube.ddd.aspect.HandleResourceException;
import cube.ddd.common.query.PagedQuery;
import cube.ddd.utils.ResourceUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * 机构服务端点
 *
 * @author yifeng
 */
@RestController
@RequestMapping("/api/ControlPanels")
@Tag(name = "控制面板-机构信息")
public class OrganizationController extends BaseController {

    private final OrganizationAppService applicationAppService;

    public OrganizationController(OrganizationAppService applicationAppService) {
        this.applicationAppService = applicationAppService;
    }

    @GetMapping("Organizations/{organizationId}")
    @Operation(summary = "通过编号获取机构")
    @HandleResourceException
    public RestResponse<OrganizationVO> getOrganization(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                      @PathVariable("organizationId") long organizationId) {
        return ResourceUtils.getById(request, response, organizationId, applicationAppService::getOrganization);
    }


    @GetMapping("Organizations/Code/{organizationCode}")
    @Operation(summary = "通过代码获取机构")
    @HandleResourceException
    public RestResponse<OrganizationVO> getOrganizationByCode(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                            @PathVariable("organizationCode") String organizationCode) {
        return ResourceUtils.getByText(request, response, organizationCode, applicationAppService::getOrganizationByCode);
    }

    @GetMapping("Organizations/Name/{organizationName}")
    @Operation(summary = "通过名称获取机构列表")
    @HandleResourceException
    public RestResponse<OrganizationVO> getOrganizationByName(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                         @PathVariable("organizationName") String organizationName) {
        return ResourceUtils.getByText(request, response, organizationName, applicationAppService::getOrganizationByName);
    }

    @GetMapping("Organizations/Available")
    @Operation(summary = "获取可用机构列表")
    @HandleResourceException
    public RestResponse<Collection<OrganizationVO>> getAvailableOrganizations(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        return ResourceUtils.getList(request, response, applicationAppService::getAvailableOrganizations);
    }

    @PostMapping("Organizations/Query")
    @Operation(summary = "分页获取机构列表")
    @HandleResourceException
    public RestResponse<Collection<OrganizationVO>> getOrganizationsByQuery(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                          @RequestBody PagedQuery query) {
        return ResourceUtils.getPagedObjectsByQuery(request, response, query, applicationAppService::getOrganizationsByQuery);
    }

    @PostMapping("Organizations")
    @Operation(summary = "创建机构")
    @HandleResourceException
    public RestResponse<OrganizationVO> createOrganization(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody CreateOrganizationCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, applicationAppService::createOrganization);
    }


    @PutMapping("Organizations")
    @Operation(summary = "更新机构")
    @HandleResourceException
    public RestResponse<OrganizationVO> updateOrganization(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody UpdateOrganizationCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, applicationAppService::updateOrganization);
    }

    @PutMapping("Organizations/Actions/Delete")
    @Operation(summary = "通过编号删除机构")
    @HandleResourceException
    public RestResponse<Boolean> deleteOrganization(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody DeleteCommand command) {
        return ResourceUtils.delete(request, response, command, applicationAppService::deleteOrganization);
    }

    @PutMapping("Organizations/Actions/Enable")
    @Operation(summary = "通过编号启用机构")
    @HandleResourceException
    public RestResponse<Boolean> enableOrganization(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody EnableCommand command) {
        return ResourceUtils.enable(request, response, command, applicationAppService::enableOrganization);
    }

    @PutMapping("Organizations/Actions/Disable")
    @Operation(summary = "通过编号禁用机构")
    @HandleResourceException
    public RestResponse<Boolean> disableOrganization(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody DisableCommand command) {
        return ResourceUtils.disable(request, response, command, applicationAppService::disableOrganization);
    }
}
